package com.space.school.controller;


import com.space.school.domain.Info;
import com.space.school.domain.User;
import com.space.school.service.InfoService;
import com.space.school.service.impl.InfoServiceImpl;
import com.space.school.service.impl.ProductListServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "InfoOperateServlet",value = "/InfoOperateServlet")
public class InfoOperateServlet extends HttpServlet {
    private InfoService infoService=new InfoServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String product_id = req.getParameter("product_id");
        String operate = req.getParameter("operate");
        if(operate.equals("del")){
            infoService.del(Integer.parseInt(product_id));

            resp.sendRedirect("/InfoServlet");
        }else if(operate.equals("update")){
            Info info=infoService.find(Integer.parseInt(product_id));
            if(info!=null){
                req.setAttribute("info",info);
                req.getRequestDispatcher("updateInfoList.jsp").forward(req,resp);
            }
        }else if(operate.equals("add")){
            ProductListServiceImpl productListService=new ProductListServiceImpl();
            req.setAttribute("categoryList",productListService.findAll());
            req.getRequestDispatcher("addInfoList.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
//      提取商品id 名称 单价 库存 描述 小图 商品状态
        String product_id = req.getParameter("product_id");
//      提取商品修改的新  名称 库存 描述 小图 商品状态
        String new_product_name=req.getParameter("new_product_name");
        String new_product_price=req.getParameter("new_product_price");
        String new_product_stock = req.getParameter("new_product_stock");
        String new_product_description = req.getParameter("new_product_description");
        String new_product_icon = req.getParameter("new_product_icon");
        String new_product_status = req.getParameter("new_product_status");
        String new_category_type = req.getParameter("new_category_type");

        if(product_id==null){
//            新增操作
            Info info=new Info();
            info.setProduct_name(new_product_name);
            info.setProduct_price(Integer.parseInt(new_product_price));
            info.setProduct_stock(Integer.parseInt(new_product_stock));
            info.setProduct_description(new_product_description);
            info.setProduct_status(Integer.parseInt(new_product_status));
            info.setCategory_type(Integer.parseInt(new_category_type));
            int a=infoService.add(info);
            if (a==1){
//                添加失败
                req.setAttribute("info_message","名字已经有了");
                req.getRequestDispatcher("addInfoList.jsp").forward(req,resp);
            }else if(a==3){
//                添加成功
                resp.sendRedirect("/InfoServlet");
            }
        }else {
//            修改操作
//
//            Map<String,String[]>map=req.getParameterMap();
//            infoService.update2(map);
//            resp.sendRedirect("/InfoServlet");


            Info info=new Info();
            info.setProduct_id(Integer.parseInt(product_id));
            info.setProduct_name(new_product_price);
            info.setProduct_stock(Integer.parseInt(new_product_stock));
            info.setProduct_description(new_product_description);
            info.setProduct_icon(new_product_icon);
            info.setProduct_status(Integer.parseInt(new_product_status));
            infoService.update(info);
            resp.sendRedirect("/InfoServlet");

        }


    }
}
